package com.xinzhi.service.impl;

import com.xinzhi.log.LoginLog;
import com.xinzhi.mapper.UserBindingMapper;
import com.xinzhi.mapper.UserMapper;
import com.xinzhi.mapper.UsersMapper;
import com.xinzhi.pojo.Bankaccount;
import com.xinzhi.pojo.User;
import com.xinzhi.pojo.Userbindingcardtable;
import com.xinzhi.pojo.dto.MyCard;
import com.xinzhi.service.IUserbingdingService;
import com.xinzhi.service.IVerifyService;
import com.xinzhi.service.UsersService;
import com.xinzhi.utils.RedisUtils;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class UserbingdingServiceImpl implements IUserbingdingService {
    @Autowired
    private UserBindingMapper userBindingMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IVerifyService verifyService;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private LoginLog loginLog;
    @Autowired
    private UsersService usersService;
    @Autowired
    private UsersMapper usersMapper;
    @Override
    public Result ifMyCard(MyCard myCard) {
        if (myCard.getCardNumber() == null)
            return Result.error("卡号不能为空");
        if (!verifyService.isCard_Number(myCard.getCardNumber()))
            return Result.error("卡号不合法");
        Bankaccount bankaccount = userBindingMapper.selectBankaccountByMycard(myCard);
        if (bankaccount == null)
            return Result.error("该银行卡不存在");
        return Result.ok(bankaccount);
    }

    @Override
    public Result addMyCard(Userbindingcardtable userbindingcardtable) {
        if (userbindingcardtable.getCardId() == null && userbindingcardtable.getUserId() == null)
            return Result.error("参数不能为空");
        Bankaccount Bankaccount = usersService.getUserByCardNumber(userbindingcardtable.getUsername(), userbindingcardtable.getCardId());
        if (Bankaccount == null)
            return Result.error("该银行卡非本人银行卡");
        List<Userbindingcardtable> a = usersMapper.selectCard(userbindingcardtable.getUserId());
        if (a==null)
            return Result.error("该银行卡已被绑定");
        userbindingcardtable.setCardId(Bankaccount.getId().toString());
        int i = userBindingMapper.userBinding(userbindingcardtable);
        return i > 0 ? Result.ok() : Result.error("绑定失败");
    }
    @Override
    public Result loginBynameAndCard(String username, String password) {
        // 调用用户绑定接口
        if (username ==null || password == null )
            return Result.error("输入不能为空");
        if (!password.matches("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"))
            return Result.error("密码格式错误");
        Bankaccount bankaccountByName;
        if (username.length()<=15){
            bankaccountByName = userBindingMapper.getBankaccountByName(username);
        }else {
            if (!verifyService.isCard_Number(username))
                return Result.error("卡号不合法");
            bankaccountByName = userBindingMapper.getBankaccountByCard(username);
        }
        User user = userMapper.selectPhone(bankaccountByName.getPhoneNumber());
        if (user ==null){
            return Result.error("该用户不存在");
        }
        String s = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!user.getPassword().equals(s))
            return Result.error("密码错误");

        if (bankaccountByName != null ){
            loginLog.insertLoginLog(user.getId(),1);
            redisUtils.set("user"+bankaccountByName.getPhoneNumber(),user,300);
            return Result.ok("登录成功");
        }
        return Result.error("用户名/卡号或密码错误");
    }
}
